<?php

namespace common\models;

use Yii;
use yii\web\IdentityInterface;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "business".
 *
 * @property integer $id
 * @property integer $account
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $auth_key
 * @property string $name
 * @property string $phone
 * @property string $logo
 * @property integer $type
 * @property string $province
 * @property string $city
 * @property string $area
 * @property string $address
 * @property string $licence
 * @property string $location
 * @property integer $share_num
 * @property integer $read_num
 * @property string $code_url
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class Business extends ActiveRecord implements IdentityInterface
{
    /** 已删除 */
    const STATUS_DELETED = -1;
    /** 未审批 */
    const STATUS_NO_APPROVAL = 0;
    /** 已审批 */
    const STATUS_APPROVAL = 1;
    /** 影楼 */
    const STATUS_YL = 1;
    /** 工作室 */
    const STATUS_GZS = 2;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['account', 'password_hash'], 'required'],
            [['share_num', 'read_num', 'status', 'created_at', 'updated_at'], 'integer'],
            [['password_hash', 'name', 'logo', 'address', 'licence', 'code_url'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['phone', 'account'], 'string', 'max' => 11],
            [['province', 'city', 'area'], 'string', 'max' => 10],
            [['location'], 'string', 'max' => 50],
            ['status', 'default', 'value' => self::STATUS_NO_APPROVAL],
            ['status', 'in', 'range' => [self::STATUS_DELETED, self::STATUS_NO_APPROVAL, self::STATUS_APPROVAL]],
            ['type', 'default', 'value' => self::STATUS_YL],
            ['type', 'in', 'range' => [self::STATUS_YL, self::STATUS_GZS]],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'account' => 'Account',
            'password_hash' => 'Password Hash',
            'auth_key' => 'Auth Key',
            'name' => 'Name',
            'phone' => 'Phone',
            'logo' => 'Logo',
            'type' => 'Type',
            'province' => 'Province',
            'city' => 'City',
            'area' => 'Area',
            'address' => 'Address',
            'licence' => 'Licence',
            'location' => 'Location',
            'share_num' => 'Share Num',
            'read_num' => 'Read Num',
            'code_url' => 'Code Url',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'password_reset_token' => 'Password Reset Token'
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        /** 去掉敏感信息 */
        unset($fields['password_hash'], $fields['password_reset_token']);
        /** 格式化日期 */
        $fields['created_at'] = function(){
            return date('Y-m-d H:i:s', $this->created_at);
        };
        $fields['updated_at'] = function(){
            return date('Y-m-d H:i:s', $this->updated_at);
        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * 密码加密
     * @param $password
     * @throws \yii\base\Exception
     * @throws \yii\base\InvalidConfigException
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }
    /**
     * 校验密码
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }


    /**
     * 查询商家 By account
     *
     * @param string $account 账号
     * @return static|null
     */
    public static function findByAccount($account)
    {
        return static::findOne(['account' => $account]);
    }
    /**
     * 查询商家 By auth_key
     *
     * @param string $account 账号
     * @return static|null
     */
    public static function findByAuthKey($auth_key)
    {
        return static::findOne(['auth_key' => $auth_key]);
    }


    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }

}
